<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use zgldh\QiniuStorage\QiniuStorage;
use App\ShopApplyHair;
use App\Shop;
use Input;
use DB;
class ShopApplyHairController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //获取所有数据
        $datas = ShopApplyHair::select('id','hairurl','remark','senddate')->paginate('20');
        return view('admin.shapply.index',compact('datas'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //返回视图层
        return view('admin.shapply.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //获取
        $shopid = Input::get('shopid');
        $hairtype = Input::get('hairtype');
        $hairsex = Input::get('hairsex');
        $remark = Input::get('remark');
        $senddate = time();
       //首先判断是否有图片传来
        if(!($request -> hasFile('file'))){
            return back() -> with(['info'=>'没有添加图片,请添加图片']);
        }else{
            $file = Input::file('file');
            $extension = strtolower($file -> getClientOriginalExtension());
            $clientName = $file -> getClientOriginalName();
            if(!in_array($extension,['png','jpg']))
            return back() -> with(['info'=>'需要png类型或者jpg类型']);
            //首先获取它的扩展名
            //上传到七牛
            $disk = QiniuStorage::disk('qiniu');
            $hairurl = 'apply/'.md5(date('Y-m-d H:i:s').$clientName).".".$extension;
            $bool = $disk -> put($hairurl,file_get_contents($file->getRealPath()));
            if($bool){
                //将数据插入数据库
                $res = ShopApplyHair::create(['shopid'=>$shopid,'hairtype'=>$hairtype,'hairsex'=>$hairsex,'remark'=>$remark,'hairurl'=>$hairurl,'senddate'=>$senddate]);
                if($res)
                return back() -> with(['info' => '信息添加成功']);
                return back() -> with(['info' => '信息添加失败']);
            }else{
                return back()->with(['info'=>'图片存入七牛失败']);
            }
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //两表联查
        $datas = DB::table('shops_apply_hair')
            ->join('shops','shops_apply_hair.shopid','=','shops.shopid')
            ->select('shops_apply_hair.*','shops.shopname')
            ->where('shops_apply_hair.id',$id)
            ->first();
       return view('admin.shapply.show',compact('datas'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //根据id选择内容
        $datas = DB::table('shops_apply_hair')
            ->join('shops','shops_apply_hair.shopid','=','shops.shopid')
            ->select('shops_apply_hair.*','shops.shopname')
            ->where('shops_apply_hair.id',$id)
            ->first();
        if(isset($datas))
        return view('admin.shapply.edit',compact('datas'));
        return back() -> with(['info'=>'数据出现错误']);


    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //进行表单验证
        $this -> validate($request,[
            'remark'=>'max:255'
        ],[
            'remark.max'=>'备注字段过长',
        ]);
        //进行数据插入;
        $remark = Input::get('remark');
        $status = Input::get('status');
        $hairtype = Input::get('hairtype');
        $res = ShopApplyHair::where('id',$id)->update(['remark'=>$remark,'status'=>$status,'hairtype'=>$hairtype]);
        if($res)
        return back() -> with(['info'=>'更新成功']);
        return back() -> with(['info'=>'更新失败']);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //删除数据
        $hairurl = ShopApplyHair::where('id',$id)->value('hairurl');
        $res = ShopApplyHair::where('id',$id)->delete();
        if($res){
            $disk = QiniuStorage::disk('qiniu');
            $bool = $disk -> delete($hairurl);
            if($bool){
                return Response() -> json(['status'=>'success','msg'=>'删除成功']);
            }else{
                return Response() -> json(['status'=>'success','msg'=>'删除七牛图片失败地址:'.$hairurl]);
            }

        }

        return Response() -> json(['status'=>'error','msg'=>'删除失败']);
    }
}
